﻿using Mapster;
using Service.Auth.Domain.AggregatesModel;
using Service.Auth.Model.Model.Dto;
using Service.Auth.Model.Model.Vo;
using Service.Core.Redis.Aop.Cache;
using Service.Core.Repository;
using Service.Framework;

namespace Service.Auth.Api.Application.Query
{
    /// <summary>
    /// 系统查询器
    /// </summary>
    /// <param name="systemRepository"></param>
    public class SystemQueryService(IRepository<TSystem> systemRepository) : IScopedService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public async Task<TSystemVo> QuerySystemByClientId(string clientId)
        {
            var list = await QuerySystemList();
            return list.FirstOrDefault(s => s.ClientId.Equals(clientId));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TSystemVo> QuerySystemById(long id)
        {
            var list = await QuerySystemList();
            return list.FirstOrDefault(s => s.Id.Equals(id));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<List<TSystemVo>> QuerySystemsByIds(List<long> ids)
        {
            var list = await QuerySystemList();
            return list.Where(s => ids.Contains(s.Id)).ToList();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [RedisCacheInterceptor($"{nameof(SystemQueryService)}:{nameof(QuerySystemList)}",cacheDuration: 60 * 60 * 12)] //AOP缓存,虚方法
        public virtual async Task<List<TSystemVo>> QuerySystemList()
        {
            var result = await systemRepository.GetListAsync();
            return result.Adapt<List<TSystemVo>>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<List<IdName<long, string>>> QuerySystemDropdown()
        {
            var result = await QuerySystemList();
            return result.Select(s => new IdName<long, string>(s.Id, s.SystemName)).ToList();
        }
    }
}